<?php

namespace App\Service\WorkOrder;

use App\Enums\WorkOrder\OrderEnum;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Models\WorkOrder\WorkOrderOperateLogModel;

class WorkOrderService extends WorkOrderBaseService
{
    /**
     * @param object $params
     */
    public static function merchantConfirmFinish(object $params)
    {
        $applyMain = WorkOrderApplyMainModel::query()->where("workOrderNo", $params->workOrderNo)->first();

        WorkOrderApplyMainModel::query()->where("workOrderNo", $params->workOrderNo)->update([
            "merchantConfirmFinishType"   => WorkOrderApplyMainModel::MERCHANT_CONFIRM_FINISH_TYPE_MERCHANT,
            "merchantConfirmFinishTime"   => time(),
            "merchantConfirmFinishStatus" => WorkOrderApplyMainModel::MERCHANT_CONFIRM_FINISH_STATUS_FINISH
        ]);

        WorkOrderOperateLogModel::generateOperateLog(
            $params->workOrderNo,
            $applyMain->step,
            WorkOrderOperateLogModel::TYPE_14,
            request()->user()->username,
            request()->user()->id,
            "商家主动确认完成工单"
        );
    }

    /**
     * @return array
     */
    public static function type(): array
    {
        return [
            "sourceType"      => enumToSelection(WorkOrderApplyMainModel::SOURCE_TYPE_TEXT),
            "problemTypeTree" => collect(OrderEnum::PROBLEM_FEEDBACK_TYPE_LIST_WEB)->map(function ($type) {
                $type["child"] = enumToSelection($type["child"]);
                return $type;
            })->values()->toArray()
        ];
    }
}
